<template>
    <div v-if="isShow">
        <div>
            <Select v-model="model" :placeholder="placeholder" :label-in-value="true" :disabled="disabled">
                <Option @click.native="handleSelectChange(item)" v-for="item in list" :value="item.ID + ''" :key="item.ID">{{ item.name }}</Option>
            </Select>
        </div>
    </div>
</template>

<script>
import { getInfo } from './service';

export default {
    name: 'AsyncSelect',
    props: {
        value: {
            type: String,
            default: ''
        },
        id: {
            type: String,
            default: ''
        },
        currentModel: {
            type: String,
            default: ''
        },
        disabled: {
            type: Boolean,
            default: false
        },
        vague: {
            type: Boolean,
            default: false
        },
        url: {
            type: String,
            default: ''
        },
        typeOf: {
            type: String,
            default: 'supplier'
        },
        theWay: {
            type: String,
            default: 'post'
        },
        placeholder: {
            type: String,
            default: '请选择'
        },
        parameter: {
            type: Object
        }
    },
    data() {
        return {
            model: '',
            list: [],
            isShow: false
        };
    },
    methods: {
        handleSelectChange(item) {
            this.$emit('setDictionaries', item, this.value);
        }
    },
    mounted() {
        this.isShow = false;
        getInfo({ url: this.url }, this.theWay, this.parameter).then(res => {
            this.list = res.data;
            for (let item in this.list) {
                if (this.typeOf == 'supplier') {
                    this.list[item].name = this.list[item].SupplierName;
                } else if (this.typeOf == 'store') {
                    this.list[item].name = this.list[item].ShopName;
                } else if (this.typeOf == 'brand') {
                    this.list[item].name = this.list[item].BrandName;
                } else if (this.typeOf == 'purchase') {
                    this.list[item].name = this.list[item].Title;
                } else if (this.typeOf == 'ApprovalCategory') {
                    this.list[item].name = this.list[item].GroupName;
                } else if (this.typeOf == 'model') {
                    this.list[item].name = this.list[item].TypeName;
                }
            }
            this.model = this.currentModel + '';
            if (this.vague) {
                this.list.unshift({
                    ID: 0,
                    name: '全部'
                });
            }
            setTimeout(() => {
                this.isShow = true;
            }, 800);
        });
    }
};
</script>

<style>
</style>
